package com.sure0000.utils;

import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.client.SystemDefaultCredentialsProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Map;

/**
 * @program: source-connect-http-poller
 * @description: apache http client 类封装
 * @author: xuyouchang@supcon.com
 * @create: 2019-12-25 13:42
 **/
public class ApacheHttpClientDemo {

    private static final Logger logger = LoggerFactory.getLogger(ApacheHttpClientDemo.class);

    private static ApacheHttpClientDemo httpUtils = new ApacheHttpClientDemo();
    private CloseableHttpClient closeableHttpClient;
    private RequestConfig requestConfig;
    private HttpClientContext httpClientContext;

    private ApacheHttpClientDemo() {
        closeableHttpClient = HttpClients.createDefault();
    }

    public static ApacheHttpClientDemo getInstance() {
        return httpUtils;
    }

    /**
     * 设置 request 请求的参数设置
     * @param requestConfig 参数配置
     */
    public void setRequestConfig(RequestConfig requestConfig) {
        this.requestConfig = requestConfig;
    }

    /**
     * 设置请求认证凭据
     * @param host 限定认证地址
     * @param port 限定认证端口
     * @param username 口令、用户名
     * @param password 密码
     */
    public void setCredentialsProvider(String host, int port, String username, String password) {
        CredentialsProvider credentialsProvider = new SystemDefaultCredentialsProvider();
        AuthScope authScope = new AuthScope(host, port);
        credentialsProvider.setCredentials(authScope, new UsernamePasswordCredentials(username, password));
        httpClientContext = new HttpClientContext();
        httpClientContext.setCredentialsProvider(credentialsProvider);
    }

    public HttpResponse doGet(String url) {
        return doGet(url, null);
    }

    /**
     * get 请求
     * @param url 请求地址
     * @param header 用户自定义 header
     * @return 目标服务器返回结果
     */
    public HttpResponse doGet(String url, Map<String, String> header) {
        HttpGet httpGet = new HttpGet(url);
        if (requestConfig != null) {
            httpGet.setConfig(requestConfig);
        }
        if (header != null) {
            header.forEach(httpGet::addHeader);
        }

        try {
            if (httpClientContext == null) {
                return closeableHttpClient.execute(httpGet);
            }
            return closeableHttpClient.execute(httpGet, httpClientContext);
        } catch (IOException e) {
            logger.error("send get request to {} fail", url, e);
            return null;
        }finally {
            httpGet.releaseConnection();
        }
    }
}
